-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.py
More file actions
29 lines (25 loc) · 872 Bytes
/
Copy pathSolution.py
File metadata and controls
29 lines (25 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def find_second_largest(arr):
if len(arr) < 2:
print("Error: Array must have at least two elements.")
return
largest = second_largest = float('-inf')
for num in arr:
if num > largest:
second_largest = largest
largest = num
elif num > second_largest and num != largest:
second_largest = num
if second_largest == float('-inf'):
print("There is no second largest element.")
else:
print("The second largest number is:", second_largest)
if __name__ == "__main__":
n = int(input("Enter the number of elements: "))
if n < 2:
print("Error: Array must have at least two elements.")
else:
arr = []
print("Enter the elements of the array: ")
for _ in range(n):
arr.append(int(input()))
find_second_largest(arr)